package com.wanxi.education.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanxi.education.mapper.ClassificationMapper;
import com.wanxi.education.model.vo.advertandcertificate.*;
import com.wanxi.education.service.ClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: yanguangqiong
 * @Description:
 * @Date: Created in 2021/9/28
 * @Version: 1.0
 */
@Service
public class ClassificationServiceImpl implements ClassificationService {
    @Autowired
    private ClassificationMapper classificationMapper;
//绑定证书分类
    @Override
    @Transactional
    public List<ClassificationForCourseVo> findAllForCourse() {
//        查找父类型
        List<ClassificationForCourseVo> dav = classificationMapper.findDav();
//        查找基础类型
        for (ClassificationForCourseVo classificationForCourseVo : dav) {
//            拿到父id
            Long davId = classificationForCourseVo.getId();
//            通过父id查找下一级类型
            List<ClassificationFrontVo> type = classificationMapper.findType(davId);
            for (ClassificationFrontVo classificationFrontVo : type) {
//                拿到基础类型
                Long typeId = classificationFrontVo.getId();
//                查找证书名称
                List<CertificateNameVo> certificateName = classificationMapper.findCertificateName(typeId);
                classificationFrontVo.setCertificateNameVos(certificateName);
            }
//            父类型的集合
            classificationForCourseVo.setCertificateNameVos(type);
        }
        return dav;
    }

//    后台查询所有
    @Override
    @Transactional
    public PageInfo findPage(ClassificationFindPageVo classificationFindPageVo) {
        PageHelper.startPage(classificationFindPageVo.getPageNum(),classificationFindPageVo.getPageSize());
        if(classificationFindPageVo.getPName()!=null){
        Long pId=classificationMapper.findPName(classificationFindPageVo.getPName());
        classificationFindPageVo.setPid(pId);
        }
        List<ClassificationVo> list= classificationMapper.findPage(classificationFindPageVo);
//        将pid为0 的类别赋值为导航类别
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getPName()==null){
                list.get(i).setPid(0L);
                list.get(i).setPName("导航类别");
            }
        }
        PageInfo<ClassificationVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

//    添加证书分类时的父类级别
    @Override
    public List<ClassificationForCourseVo> findAll() {
//        查找父类型
        List<ClassificationForCourseVo> dav = classificationMapper.findDav();
//        查找基础类型
        for (ClassificationForCourseVo classificationForCourseVo : dav) {
//            拿到父id
            Long davId = classificationForCourseVo.getId();
//            通过父id查找下一级类型
            List<ClassificationFrontVo> type = classificationMapper.findType(davId);
            classificationForCourseVo.setCertificateNameVos(type);
        }
        return dav;
    }

    @Override
    public void add(ClassificationAddVo classificationAddVo) {
        classificationMapper.add(classificationAddVo);
    }

    @Override
    public void update(ClassificationAddVo classificationAddVo) {
        classificationMapper.update(classificationAddVo);
    }

    @Override
    public void delete(Long classificationId) {
        classificationMapper.delete(classificationId);
    }

//    查询分类详情
    @Override
    public ClassificationVo findById(Long classificationId) {
        ClassificationVo classificationVo= classificationMapper.findById(classificationId);
        return classificationVo;
    }

//    查询导航
    @Override
    public PageInfo<ClassificationFrontVo> findNavigation() {
        PageHelper.startPage(0,6);
        List<ClassificationFrontVo> classificationFrontVos=classificationMapper.findNavigation();
        PageInfo<ClassificationFrontVo> pageInfo = new PageInfo<>(classificationFrontVos);
        return pageInfo;
    }

    @Override
    public List<ClassificationFrontVo> findByPid(Long pid) {
        List<ClassificationFrontVo> list =classificationMapper.findPid(pid);
        return list;
    }

    public ClassificationForCourseVo findByCCIEId(Long ccieId){
//        拿到证书名称 和id
        List<CertificateNameVo> certificateNameVo = classificationMapper.findByCCIEId(ccieId);
//        拿到基本类型的名称和id
        List<ClassificationFrontVo> classificationFrontVo =classificationMapper.findCertificateId(certificateNameVo.get(0).getClassificationId());
        for (int i = 0; i < classificationFrontVo.size(); i++) {
            classificationFrontVo.get(i).setCertificateNameVos(certificateNameVo);
        }
//        拿到父类型的名称和id
        ClassificationForCourseVo classificationForCourseVo=classificationMapper.findByPid(classificationFrontVo.get(0).getPid());
        classificationForCourseVo.setCertificateNameVos(classificationFrontVo);
        return classificationForCourseVo;
    }
}
